setwd("../")
rm(list = ls())

# # only need to install once
# devtools::install_github("cloudyr/pyMTurkR", dependencies=TRUE)
# devtools::install_github("Luwei-Ying/validateIt", dependencies=TRUE)
# load R package
library(validateIt)

### load documents with predicted topics
top3domText <- read.csv("textpool/top3domText.csv", stringsAsFactors = FALSE)

### prepare tasks
LIbroadcareful <- validateLabel(type = "LI", n = 500, 
                             text.predict = top3domText, text.name = "post_text",
                             labels = c("Equal Pay for Women",
                                        "Healthcare/Reproductive Rights",
                                        "Agriculture",
                                        "Student Loan/Debt",
                                        "Drug Abuse",
                                        "Higher Education/Job Training",
                                        "Wall Street/Financial Sector",
                                        "Government Shutdown/Congressional Budget",
                                        "Obamacare/Tax Policy",
                                        "Deficits/Debt/Budget"), 
                             labels.index = c(1, 9, 21, 35, 44, 62, 65, 70, 88, 89), 
                             labels.add = c("International Trade",
                                            "Praising Active Military/Military Units",
                                            "Terrorism",
                                            "Military Sexual Assault",
                                            "Nuclear Deterrance/International Security",
                                            "Air Force",
                                            "Honoring Specific Veterans",
                                            "Honoring Veterans/Heroes",
                                            "Military Operations/Armed Conflicts",
                                            "Veterans Affairs/Veterans Healthcare"))

goldLI <- read.csv("goldstandard/goldLI.csv", stringsAsFactors = FALSE)
LIbroadcareful <- mixGold(tasks = LIbroadcareful, golds = goldLI)
LIbroadcareful <- recordTasks(type = "LI", tasks = LIbroadcareful,
                              path = "labelrecords/LIbroadcareful.Rdata")

# ##### interact with Mturk from here #####
# ### environment
# library(pyMTurkR)
# Sys.setenv(AWS_ACCESS_KEY_ID = "AWS_ACCESS_KEY_ID")
# Sys.setenv(AWS_SECRET_ACCESS_KEY = "AWS_SECRET_ACCESS_KEY")
# options(pyMTurkR.sandbox = T) # Change sandbox = F when ready to run on MTurk
# AccountBalance()
# 
# ### send tasks (first trial)
# LIbroadcarefulids <- sendTasks(hit_type = 'find_from_dashboard', hit_layout = 'find_from_dashboard', type = "LI",
#                                tasksrecord = LIbroadcareful, HITidspath = "HITids/LIbroadcarefulids1.Rdata")
# ### extend HITs if needed
# for(i in LIbroadcarefulids[[1]]){
#   ExtendHIT(hit = i, add.seconds = 60*60*26)
# }
# 
# ### get results
# load("HITids/LIbroadcarefulids1.Rdata")
# LIbroadcarefulresults1 <- getResults(batch_id = "LIbroadcareful1", hit_ids = HITids, retry = FALSE)
# write.csv(LIbroadcarefulresults1, "LI/results/LIbroadcarefulresults1.csv", row.names = FALSE)
# evalResults(results = LIbroadcarefulresults1, key = LIbroadcareful, type = "LI")
# 
# ### send tasks (second trial)
# LIbroadcarefulids <- sendTasks(hit_type = 'find_from_dashboard', hit_layout = 'find_from_dashboard', type = "LI",
#                                tasksrecord = LIbroadcareful, HITidspath = "HITids/LIbroadcarefulids2.Rdata")
# ### extend HITs if needed
# for(i in LIbroadcarefulids[[1]]){
#   ExtendHIT(hit = i, add.seconds = 60*60*26)
# }
# 
# ### get results
# load("HITids/LIbroadcarefulids2.Rdata")
# LIbroadcarefulresults2 <- getResults(batch_id = "LIbroadcareful2", hit_ids = HITids, retry = FALSE)
# write.csv(LIbroadcarefulresults2, "LI/results/LIbroadcarefulresults2.csv", row.names = FALSE)
# evalResults(results = LIbroadcarefulresults2, key = LIbroadcareful, type = "LI")